package bll

var StateStorage = NewStateStorage()

// IStateChangeSink is the interface that wraps the basic init method.
type IStateChangeSink interface {
	// OnStateChange 状态变更
	OnStateChange(topic, metaType, key string, value interface{}) error
}

type stateStorage struct {
	storage *storage
}

func NewStateStorage() *stateStorage {
	return &stateStorage{
		storage: NewStorage(),
	}
}

func (s *stateStorage) Init() error {
	if err := s.storage.Start(); err != nil {
		panic(err)
	}
	return nil
}

// Put 存储数据
func (s *stateStorage) Put(stateType StateType, deviceName, metaType, key string, value interface{}) error {
	// Todo: 存储数据
	err := s.storage.Put(stateType, deviceName, metaType, key, value)
	if err != nil {
		return err
	}

	// TODO: 通知状态变更
	return nil
}

// Get 获取数据
func (s *stateStorage) Get(deviceName string) (interface{}, error) {
	return s.storage.GetShadow(deviceName)
}

// GetStateShadow 获取影子数据
func (s *stateStorage) GetStateShadow(stateType StateType, deviceName string) (interface{}, error) {
	return s.storage.GetStateShadow(stateType, deviceName)
}

// GetMetaTypeShadow 获取影子数据
func (s *stateStorage) GetMetaTypeShadow(stateType StateType, deviceName string, metaType string) (interface{}, error) {
	return s.storage.GetMetaTypeShadow(stateType, deviceName, metaType)
}

// GetShadowValue 获取影子数据
func (s *stateStorage) GetShadowValue(stateType StateType, deviceName, metaType, key string) (interface{}, error) {
	return s.storage.GetShadowValue(stateType, deviceName, metaType, key)
}

// Delete 删除数据
func (s *stateStorage) Delete(deviceName, metaType, key string) error {
	// Todo: 查看缓存中是否存在
	// Todo: 查看redis缓存中是否存在
	// Todo: 删除数据
	return nil
}

// OnStateChange 状态变更
func (s *stateStorage) OnStateChange(deviceName, metaType, key string, value interface{}) error {
	// Todo: 分析topic
	// Todo: 返回设备影子数据
	return nil
}
